<?php
namespace wechat\controllers;

use common\enums\StatusEnum;
use common\models\store\Store;
use Yii;
use common\components\WechatLoginTrait;
use common\controllers\BaseController;

/**
 * 微信基类
 *
 * Class WController
 * @package wechat\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class WController extends BaseController
{
    use WechatLoginTrait;

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function init()
    {
        parent::init();
        $request = Yii::$app->request;
        $store_id = $request->get('store_id')??0;

        if(!$store_id)
            die('店铺id不能为空');

        //查询店铺是否存在
        $store = Store::find()->where(['id'=>$store_id])->asArray()->one();

        if(!$store)
            die('店铺不存在');
        else if($store['status'] != StatusEnum::ENABLED)
            die('店铺关闭中');

        $merchant_id = $store['member_id'];

        if (!Yii::$app->wechat->isWechat)
        {
//             die('请用微信打开');
        }

        // 修改微信授权方式为静默授权
        // Yii::$app->params['wechatConfig']['oauth']['scopes'] = ['snsapi_base'];

        // 开启微信模拟数据
        Yii::$app->params['simulateUser']['switch'] = true;

        // 微信登录
        $this->login($store_id, $merchant_id);
    }
}
